package service;

import dao.RoleDao;
import dao.RoleDaoImpl;
import dao.UsersDao;
import dao.UsersDaoImpl;
import pojo.Menu;
import pojo.Role;
import pojo.Student;
import pojo.Users;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UsersServicesImpl implements UsersService{
    UsersDao usersDao = new UsersDaoImpl();
    RoleDao roleDao = new RoleDaoImpl();
    @Override
    public Users selectUsersByName(String loginName) throws SQLException {
        Users users = usersDao.selectUsersByName(loginName);
//        System.out.println(users);
        //根据角色id查询角色，菜单信息（三表联查）
        Integer roleId = users.getRoleId();
        Role role = roleDao.findByid(roleId);
        //需要对菜单进行分级
        List<Menu> menuList = role.getMenuList();
        List<Menu> newMenuList = new ArrayList<>();//保存分级后的菜单
        for (Menu menu : menuList) {
            if (menu.getUpmenuId()==0){//说明是一级菜单
                List<Menu> secondList = new ArrayList<>();
                for (Menu menu1 : menuList) {
                    if(menu1.getUpmenuId()==menu.getMenuId()){
                        secondList.add(menu1);
                    }
                }
                menu.setSecondList(secondList);
                newMenuList.add(menu);
            }
        }
        role.setMenuList(newMenuList);
        users.setRole(role);
        return users;
    }

    @Override
    public List<Users> selectAll(Integer pageIndex, Integer pageSize) throws SQLException {
        return usersDao.selectAll(pageIndex,pageSize);
    }

    @Override
    public int total() throws SQLException {
        return usersDao.total();
    }

    @Override
    public int addUser(Users user) {
        return usersDao.addUser(user);
    }

    @Override
    public int updateUser(Users user) {
        return usersDao.updateUser(user);
    }

    @Override
    public Users selectById(int uid) {
        return usersDao.selectById(uid);
    }

    @Override
    public int deleteById(int uid) {
        return usersDao.deleteById(uid);
    }

    @Override
    public Users selectByuName(String loginname) {
        return usersDao.selectByuName(loginname);
    }
}
